package net.guruqu.rvoView;

import java.util.LinkedList;

import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Line;
import com.jme.scene.Node;
import com.jme.scene.Point;

public class RoadMapUnit {
	LinkedList<Vector2f> vertices;
	float vertRadius;
	float lineWidth;
	ColorRGBA lineColor;
	ColorRGBA vertexColor;

	ColorRGBA done_lineColor;
	ColorRGBA done_vertexColor;

	Node renderNode;
	boolean editing;
	Line renderLine;
	LinkedList<Line> renderVertex;

	{
		vertRadius = 10f;
		lineWidth = 5f;
		lineColor = new ColorRGBA(1, 0, 0, 0.5f);
		vertexColor = new ColorRGBA(0, 1, 0, 0.5f);

		done_lineColor = new ColorRGBA(1, 1, 1, 0.3f);
		done_vertexColor = new ColorRGBA(0, 1, 1, 0.3f);
		renderVertex = new LinkedList<Line>();
	}

	public int pick(float x, float y, float scale) {
		int id = 0;
		for (Vector2f v2f : vertices) {
			if ((v2f.x - x) * (v2f.x - x) + (v2f.y - y) * (v2f.y - y) < vertRadius
					* vertRadius*scale*scale) {
				return id;
			}
			id++;
		}
		return -1;
	}

	public RoadMapUnit() {
		vertices = new LinkedList<Vector2f>();
		renderNode = new Node("Road Unit");

	}

	public void addVertices(Vector2f nv) {
		vertices.add(nv);
		updateNode();
	}

	public void updateVertex(int id, Vector2f nv) {
		vertices.get(id).set(nv);
		updateNode();
	}

	public boolean empty() {
		return vertices.size() == 0;
	}

	public Vector2f getVertex(int id) {
		return vertices.get(id);
	}

	public void removeVertex(int id) {
		vertices.remove(id);
		updateNode();
	}

	public Node getRenderNode() {
		return renderNode;
	}

	public void updateNode() {
		renderNode.detachAllChildren();

		if (vertices.size() > 1) {
			Vector3f[] vert3f = new Vector3f[vertices.size()];
			ColorRGBA[] col3f = new ColorRGBA[vertices.size()];
			int i = 0;
			for (Vector2f v2f : vertices) {
				vert3f[i] = new Vector3f(v2f.x, 0, v2f.y);
				col3f[i] = editing ? lineColor : done_lineColor;
				i++;
			}
			Line line = new Line("connectedLine", vert3f, null, col3f, null);
			line.setLineWidth(lineWidth);
			if (editing) {
				line.setMode(Line.Mode.Connected);
			} else {
				line.setMode(Line.Mode.Loop);
			}
			renderNode.attachChild(line);
		}
		int i = 0;

		Vector3f[] p3f = new Vector3f[vertices.size()];
		ColorRGBA[] c3f = new ColorRGBA[vertices.size()];
		for (Vector2f v2f : vertices) {
			p3f[i] = new Vector3f(v2f.x, 10, v2f.y);
			c3f[i] = editing ? vertexColor : done_vertexColor;
			i++;
		}
		Point point = new Point("Vertex(" + i + ")", p3f, null, c3f, null);
		point.setPointSize(vertRadius);
		renderNode.attachChild(point);
		renderNode.updateModelBound();
		renderNode.updateRenderState();
	}

	public boolean isEditing() {
		return editing;
	}

	public void setEditing(boolean editing) {
		this.editing = editing;
		updateNode();
	}

	public LinkedList<Vector2f> getVertices() {
		return vertices;
	}
}
